﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gustozzi.DAL;
using System.Text;
namespace WebGustozzi.ModuloVentas
{
    public partial class wfBuscarCliente : System.Web.UI.Page
    {

        VentasDataEntities contexto;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Inicializar();
            }
            else
            {
                hdf_InicioGrilla.Value = "N";
            }              
        }

        protected void imbCerrar_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("../wfPrincipal.aspx");
        }

        public void Inicializar()
        {
            if (contexto == null)
            {
                contexto = new VentasDataEntities();
            }

            var listaTipoDoc = from p in contexto.TiposDocumentosPersona
                               select p;

            ddlTipoDocumento.DataSource = listaTipoDoc;
            ddlTipoDocumento.DataMember = "codigoTipoDocumentoPersona";
            ddlTipoDocumento.DataValueField = "codigoTipoDocumentoPersona";
            ddlTipoDocumento.DataTextField = "nombre";
            ddlTipoDocumento.DataBind();

            hdf_InicioGrilla.Value = "S";
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            if (contexto == null)
            {
                contexto = new VentasDataEntities();
            }
            string numDoc = txtNumeroDocumento.Text.Trim();
            if (numDoc.Equals(string.Empty))
            {
                numDoc = null;
            }
            string nombre = txtNombreRazonSocial.Text.Trim();
            if (nombre.Equals(string.Empty))
            {
                nombre = null;
            }
            string apePat = txtApellidoPaterno.Text.Trim();
            if (apePat.Equals(string.Empty))
            {
                apePat = null;
            }
            string apeMat = txtApellidoMaterno.Text.Trim();
            if (apeMat.Equals(string.Empty))
            {
                apeMat = null;
            }

            var listaClientes = contexto.BuscarCliente(
                               Convert.ToInt32(ddlTipoDocumento.SelectedValue),
                               numDoc,
                               nombre,
                               apePat,
                               apeMat);

            grvListaClientes.DataSource = listaClientes;
            grvListaClientes.DataBind();

        }

        protected void grvListaClientes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (hdf_InicioGrilla.Value == "N")
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(this.grvListaClientes, "Select$" + e.Row.RowIndex.ToString());
                    
                    BuscarCliente_Result oItem = (BuscarCliente_Result)e.Row.DataItem;

                    StringBuilder strConstructor = new StringBuilder();

                    string sNavigateURL = String.Format("window.opener.document.getElementById('CPHPrincipal_txtCodCliente').value=\"{0}\"; ",
                             oItem.codigoCliente);

                    strConstructor.Append(sNavigateURL);
                    strConstructor.Append("window.close();");


                    ((LinkButton)(e.Row.Cells[8].Controls[0])).Attributes.Add("onclick", strConstructor.ToString());
                }
            }
        }


      


    }
}